(function() {
	Wallet.widget.AccountRecordFormPanel = function(el, config) {
		Wallet.lang.applyIf(config, {
			width: "320px",
			modal: false,
			constrainToViewport: true,
			fixedCenter: true,
			visible: false,
			postmethod: "async",
			buttons : [{
				text:"Submit",
				isDefault: true,
				handler: function() {
					return function() {
							this.submit();
						}
					}()
			}, {
				text:"Cancel",
				handler: function() {
					return function() {
						this.hide();
					}
				}()
			}]
		});
		
		Wallet.widget.AccountRecordFormPanel.superclass.constructor.call(this, el, config);
	};
	
	var Lang = YAHOO.lang,
		Dom = YAHOO.util.Dom,
		Panel = YAHOO.widget.Panel,
		Dialog = YAHOO.widget.Dialog,
		AccountRecordFormPanel = Wallet.widget.AccountRecordFormPanel,
		AccountRecordCategoryPanel = Wallet.widget.AccountRecordCategoryPanel;
		
	AccountRecordFormPanel.CALENDAR_BUTTON_CSS = "calendar-button";
	AccountRecordFormPanel.CALENDAR_CONTAINER_CSS = "canlendar-container";
	AccountRecordFormPanel.CALENDAR_INPUT_CSS = "calendar-input";
	
	AccountRecordFormPanel.CATEGORY_SELECT_CSS = "category-select";
	AccountRecordFormPanel.CATEGORY_BUTTON_CSS = "category-button";
	
	AccountRecordFormPanel.CATEGORY_PANEL_CSS = "account-record-category-panel";
	
	Lang.extend(AccountRecordFormPanel, Dialog, {
		init: function(el, userConfig) {
			AccountRecordFormPanel.superclass.init.call(this, el);
			
			this.beforeInitEvent.fire(AccountRecordFormPanel);
            
            if (userConfig) {
                this.cfg.applyConfig(userConfig, true);
            }
            
            this.initComponents();
            this.initListener();
            
            this.initEvent.fire(AccountRecordFormPanel);			
		},
		initDefaultConfig: function() {
			AccountRecordFormPanel.superclass.initDefaultConfig.call(this);
		},
		initComponents: function() {
			var categorySelectEl = Dom.getElementsByClassName(AccountRecordFormPanel.CATEGORY_SELECT_CSS, "select", this.body)[0];
			this.categorySelectEl = categorySelectEl;
			
			var categoryPanelEl = Dom.getElementsByClassName(AccountRecordFormPanel.CATEGORY_PANEL_CSS, "div", this.element)[0];
			document.body.appendChild(categoryPanelEl);
			var categoryPanel = new AccountRecordCategoryPanel(categoryPanelEl);		
			
			categoryPanel.render();
			Wallet.widget.OverlayManager.register(categoryPanel);
			categoryPanel.hide();
			this.categoryPanel = categoryPanel;
			
			var categoryButtonEl = Dom.getElementsByClassName(AccountRecordFormPanel.CATEGORY_BUTTON_CSS, "button", this.body)[0];
			var categoryButton = new YAHOO.widget.Button(categoryButtonEl, {
				label: "Add",
				className: "icon-only-button add-icon",
				onclick: {
					fn: function(evt, target) {
						this.categoryPanel.show();
						Wallet.widget.OverlayManager.bringToTop(this.categoryPanel);
					},
					scope: this
				}
			});
			
			this.categoryButton = categoryButton;
			
			var calendarContainerEl = Dom.getElementsByClassName(AccountRecordFormPanel.CALENDAR_CONTAINER_CSS, "div", this.body)[0];
			var calendarContainer = new YAHOO.widget.Overlay(calendarContainerEl);
			calendarContainer.render();	
			calendarContainer.align();

			calendarContainer.body.id = calendarContainerEl.id + "_c";	
			Dom.setStyle(calendarContainer.body, "border", "1px solid #808080");
			
			var calendar = new YAHOO.widget.Calendar(calendarContainer.body.id, {
				mindate: "1/1/2006",   
				maxdate: "12/31/2008"			
			});
			
			var dateInput = Dom.getElementsByClassName(AccountRecordFormPanel.CALENDAR_INPUT_CSS, "input", this.body)[0];
			
			function onEventSelect(type, args, obj) {
				var dates = args[0];   
				var date = dates[0];   
				var year = date[0], month = date[1], day = date[2];   
				 
				dateInput.value = [year, "-", month, "-", day].join("");
				calendar.hide();			
			}
			
			calendar.selectEvent.subscribe(onEventSelect, calendar, true);
			
			var calendarButtonEl = Dom.getElementsByClassName(AccountRecordFormPanel.CALENDAR_BUTTON_CSS, "button", this.body)[0];
			var calendarButton = new YAHOO.widget.Button(
				calendarButtonEl, {
				label: "Choose",
				className: "icon-only-button view-icon",
				onclick: {
					fn: function() {
						calendar.show();
					}
				}
			});
			
			calendar.render();
			calendar.hide();
		},
		initListener: function() {
			this.categoryPanel.valueSubmitEvent.subscribe(this.onEventCategoryAdd, this);
		},
		onEventCategoryAdd: function(evt, data, obj) {
			obj.addNewCategory(data);
		},
		addNewCategory: function(category) {
			var opt = document.createElement("option");
			opt.value = category;
			opt.innerHTML = category;
			
			this.categorySelectEl.appendChild(opt);
			this.categorySelectEl.value = category;
		}		
	}); 
}());